package com.example.weatherapp.beans;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class DailyItemBean {

    private String week;
    private String date;
    private String skycon;
    private String tempMax;
    private String tempMin;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getSkycon() {
        return skycon;
    }

    public void setSkycon(String skycon) {
        this.skycon = skycon;
    }

    public String getTempMax() {
        return tempMax;
    }

    public void setTempMax(String tempMax) {
        this.tempMax = tempMax;
    }

    public String getWeek() {
        return week;
    }

    public void setWeek(String week) {
        this.week = week;
    }

    public String getTempMin() {
        return tempMin;
    }

    public void setTempMin(String tempMin) {
        this.tempMin = tempMin;
    }

    public DailyItemBean(String week, String date, String skycon, String tempMax, String tempMin) {
        this.week = week;
        this.date = date;
        this.skycon = skycon;
        this.tempMax = tempMax;
        this.tempMin = tempMin;
    }

    public static List<DailyItemBean> getList(WeatherDaily weatherDaily) {
        List<DailyItemBean> list = new ArrayList<>();
        for (int i = 0; i < weatherDaily.getResult().getDaily().getTemperature().size(); i++) {
            String date = weatherDaily.getResult().getDaily().getTemperature().get(i).getDate().substring(5, 10);
            String skycon = weatherDaily.getResult().getDaily().getSkycon().get(i).getValue();
            String tempMax = (int) weatherDaily.getResult().getDaily().getTemperature().get(i).getMax() + "\u00AD°C";
            String tempMin = (int) weatherDaily.getResult().getDaily().getTemperature().get(i).getMin() + "\u00AD°C";
            list.add(new DailyItemBean(date2Week(weatherDaily.getResult().getDaily().getTemperature().get(i).getDate().substring(0, 10)), date, skycon, tempMax, tempMin));
        }
        return list;
    }

    private static String date2Week(String date) {
        String week = "";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(date, formatter);
            LocalDate today = LocalDate.now();
            if (today.getDayOfMonth() == localDate.getDayOfMonth()) {
                week = "今天";
            } else {
                DayOfWeek dayOfWeek = localDate.getDayOfWeek();
                switch (dayOfWeek) {
                    case MONDAY:
                        week = "星期一";
                        break;
                    case TUESDAY:
                        week = "星期二";
                        break;
                    case WEDNESDAY:
                        week = "星期三";
                        break;
                    case THURSDAY:
                        week = "星期四";
                        break;
                    case FRIDAY:
                        week = "星期五";
                        break;
                    case SATURDAY:
                        week = "星期六";
                        break;
                    case SUNDAY:
                        week = "星期日";
                        break;
                }
            }
        }
        return week;
    }
}
